#! /usr/bin/perl -w # for syntax highlighting
# CSS-V1

use gri_secmgr qw(get_cookie_value got_cookie);
use gri_database;
use Data::Dumper;

sub mm_docrepos_header {
my $section=shift;

	my $db_dir=$::GRI_FRONTEND->get_global_directory("database");
	my $docrepos=$::GRI_FRONTEND->get_global_directory("docrepos");
	my $DB=new gri_database("$db_dir/gri.db");
	my $no_header=0;
	my $no_navigation=0;
	if(defined($section)) {
		$section=decode_base64($section);
		my ($a,$b)=$DB->get_section(section=>$section);
		if(defined($b) && defined($b->{attributes})) {
			my @B=split(/,/,$b->{attributes});
			if(grep {lc($_) eq "no_navigation"} @B) {
				$no_navigation=1;
			}
			if(grep {lc($_) eq "no_header"} @B) {
				$no_header=1;
			}
		}
	}
	my $username="public";
	if(! got_cookie(name => "gri_dbm_intranet")) {
		$username=undef;
	} else {
		my $v=get_cookie_value(name=>"gri_dbm_intranet");
		$v=~ s/\s+.*//;
		$username=$v;
	}
	my $left_image=$::GRI_FRONTEND->get_variable("LEFT_HEADER_LOGO");
	my $middle_image=$::GRI_FRONTEND->get_variable("MIDDLE_HEADER_LOGO");
	my $right_image=$::GRI_FRONTEND->get_variable("RIGHT_HEADER_LOGO");
	my $title=$::GRI_FRONTEND->get_variable("TITLE");
	my $left_link=$::GRI_FRONTEND->get_variable("LEFT_HEADER_LINK");
	my $middle_link=$::GRI_FRONTEND->get_variable("MIDDLE_HEADER_LINK");
	my $right_link=$::GRI_FRONTEND->get_variable("RIGHT_HEADER_LINK");
	$left_link=undef if $left_link =~ /not defined/;
	$middle_link=undef if $middle_link =~ /not defined/;
	$right_link=undef if $right_link =~ /not defined/;

	# Get list of groups that the user belongs too...
	my ($a,$b)=(0,[]);
	my ($a2,$b2)=(0,[]);
	my $css_style=undef;
	if(defined($username)) {
		($a,$b)=$DB->get_user_sections(user=>$username,type=>"reader");
		($a2,$b2)=$DB->get_user_sections(user=>$username,type=>"publisher");
	}
	my %SS=();
	$SS{$_}=1 for (@$b);
	$SS{$_}=1 for (@$b2);

	my $d;
	$d=link_to_str("","","","") . "/images";
	# Don't need the program name so get it out...
	$d =~ s/\/index.cgi//;

	my $lref=$::GRI_FRONTEND->get_portal_list();
	my @L=(@$lref);
	# Generate a drop down for the list of portals...
	# my $ss="<SELECT NAME=\"gotoportal\" onChange=\"dropdown(this)">\">\n";
	my $ss="<font class=stdtextlarge>Go to Portal: <SELECT font-size:small NAME=\"gotoportal\"><OPTION VALUE=\"__NONE__\">Choose...\n";
	my $c=0;
	if(defined($username)) {
		for my $portal (@L) {
			my $a=$::GRI_FRONTEND->is_user_allowed($portal,$username);
			if($a) {
				$c++;
				my $n=string_to_hex($portal);
				$ss.="<OPTION VALUE=\"$n\">$portal\n";
			}
		}
	}
	if(!$c) {
		$ss="";
	} else {
		$ss.="</SELECT></font>";
	}

	# Also generate a drop down for available document sections,
	# if any are available ...
	my $ss2="<font class=stdtextlarge>Go to Document section: <SELECT NAME=\"gotosection\"><OPTION VALUE=\"__NONE__\">Choose...\n";
	$c=0;
	@L=keys %SS;
	if(defined($username)) {
		for my $csec (@L) {
			my ($c1,$c2)=$DB->get_section(section=>$csec);
			if($c1) {
				$c++;
				my $n=string_to_hex($c2->{name});
				$ss2.="<OPTION VALUE=\"$n\">$c2->{title}\n";
			}
		}
	}
	if(!$c) {
		$ss2="";
	} else {
		$ss2.="</SELECT></font>";
	}

	my $ss3="";
	if(length($ss) || length($ss2)) {
		$ss3="&nbsp;&nbsp;&nbsp;&nbsp;<INPUT CLASS=\"gobutton\" TYPE=SUBMIT VALUE=\"Go\">";
	}
	
	my $ll1=link_to_str("stdlink","Search","form_docrepos_search","stype=reports");
	my $ll2=link_to_str("stdlink","Alerting","manage_alerts","");
	my $ll3=link_to_str("stdlink","Preferences","manage_prefs","");
	my $ll4=link_to_str("stdlink","Administration","admin_page","");
	if(!defined($username) || $username eq "public") {
		$ll1=$ll2=$ll3=$ll4="";
	}
	my $str="";
	if(defined($username) && $username ne "") {
		if(allow_logout() eq "no") {
			$str="<font class=tableCell>Logged in as: $username</font>";
		} else {
			my $logout=link_to_str("stdlink","Log Out","logout","");
			$str="<font class=stdtext>Logged in as: $username | </font>$logout";
		}
	} else {
		$ss=$ss2=$ss3="";
	}
	if(length($ss)) {
		$ss2="&nbsp;&nbsp;&nbsp;&nbsp;$ss2";
	}
	my $_ss="";
	if(length($ss3)) {
			load_macro('mm_embed_form_link');
			my $s=mm_embed_form_link('menu_portal_or_section_chooser');
			$_ss="<form action=\"$s\" method=\"POST\" onsubmit=\"javascript:disableForm()\">";
	}

	my $bb="<HEAD>
<SCRIPT TYPE=\"text/javascript\">
<!--
function dropdown(mySel)
{
var myWin, myVal;
myVal = mySel.options[mySel.selectedIndex].value;
if(myVal)
   {
   if(mySel.form.target)myWin = parent[mySel.form.target];
   else myWin = window;
   if (! myWin) return true;
   myWin.location = myVal;
   }
return false;
}
//-->
</SCRIPT>
<style type=\"text/css\">
<!--
     .floating {position:absolute; height:70px; width:40px; top:1; visibility:visible; z-index:999;}
     -->
</style>
</HEAD>
<BODY>";
	my $l=link_to_str("stdlink","","","");
	if(defined($middle_link)) { $l=$middle_link; }
	my $sss="<HTML>
<TITLE>
$title
</TITLE>
$bb
<table cols=3 width=100% BORDER=0 CELLSPACING=0>\n";
if($no_header==0) {
 $sss.="<tr class=gri_clearrow bgcolor=#FFFFFF>
  <td class=gri_cell>";
	if(defined($left_image) && length($left_image)) {
		if(defined($left_link)) {
			$sss.="<a href=\"$left_link\"><img src=\"$d/$left_image\" BORDER=0></a></td>";
		} else {
			$sss.="<img src=\"$d/$left_image\" BORDER=0></td>";
		}
	} else {
		$sss.="&nbsp;</td>";
	}
	if(defined($middle_image) && length($middle_image)) {
		$sss.="<td class=gri_cell-center><a href=\"$l\"><img src=\"$d/$middle_image\" BORDER=0></a></td>";
	} else {
		$sss.="<td class=gri_cell-center>&nbsp;</td>";
	}
	if(defined($right_image) && length($right_image)) {
		if(defined($right_link)) {
			$sss.="<td class=gri_cell-right><a href=\"$right_link\"><img src=\"$d/$right_image\" BORDER=0></a></td>";
		} else {
			$sss.="<td class=gri_cell-right><img src=\"$d/$right_image\" BORDER=0></td>";
		}
	} else {
		$sss.="<td class=gri_cell-right>&nbsp;</td>";
	}
	$sss.="</tr>\n";
}
	if($no_navigation==0 && defined($username) && length($username)) {
	$sss.="
</table>
<table cols=3 width=100% border=0 borderwidth=0>
 <tr class=gri_clearrow>
  <td class=gri_cell colspan=2>$_ss$ss$ss2$ss3</td>
  <td class=gri_cell-right colspan=1>$str</td>
 </tr>
 <tr class=gri_clearrow>
  <td class=gri_cell-center colspan=3>$ll1&nbsp;&nbsp;$ll2&nbsp;&nbsp;$ll3&nbsp;&nbsp;$ll4</td>
 </tr>
</table>
";
	}
	$sss.="</table></form>\n";
	return $sss;
}

# vim: ts=4:sw=4
